package com.auto.learning.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.auto.learning.R;
import com.auto.learning.bean.BookBean;
import com.auto.learning.event.Handle;
import com.auto.learning.service.MusicPlayManager;
import com.nostra13.universalimageloader.core.ImageLoader;

public class CollectPageAdapter extends BaseAdapter {

	private Context context;
	private ArrayList<BookBean> list;
	private Handle handle;
	private boolean isShowCheckBox = false;
	private ArrayList<Integer> selectedBookIds = new ArrayList<Integer>();
	private ArrayList<String> imageUrl = new ArrayList<String>();
	private int type = 1; //默认为1,2时显示已读图标
	
	public void setType(int type) {
		this.type = type;
	}

	public CollectPageAdapter(Context context,ArrayList<BookBean> list, ArrayList<String> imageUrl, Handle handle){
		this.context = context;
		this.list = list;
		this.imageUrl = imageUrl;
		this.handle = handle;
	}
	
	@Override
	public int getCount() {
		return list.size() + ((imageUrl != null && imageUrl.size() > 0) ? 1 : 0);
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}
	
	ViewHolder holder;
	ViewHolderHasRead viewHolderHasRead;
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		if (position == 0 && imageUrl.size() > 0) {
			if (convertView != null && convertView.getTag() instanceof ViewHolderHasRead) {
				viewHolderHasRead = (ViewHolderHasRead) convertView.getTag();
			}else {
				convertView = LayoutInflater.from(context).inflate(R.layout.collect_book_item_has_read, null);
				viewHolderHasRead = new ViewHolderHasRead();
				viewHolderHasRead.gv_read_book = (GridView) convertView.findViewById(R.id.gv_read_book);
				viewHolderHasRead.image_read_icon = (ImageView) convertView.findViewById(R.id.image_read_icon);
				convertView.setTag(viewHolderHasRead);
			}
			viewHolderHasRead.gv_read_book.setAdapter(new CommonAdapter<String>(context, imageUrl ,R.layout.collect_book_item_has_read_image) {

				@Override
				public void convert(com.auto.learning.adapter.ViewHolder viewHolder, String item) {
					ImageLoader.getInstance().displayImage(item, (ImageView)viewHolder.getView(R.id.image_book));
				}
			});
			viewHolderHasRead.gv_read_book.setSelector(new ColorDrawable(Color.TRANSPARENT));
			viewHolderHasRead.image_read_icon.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					handle.obtainMessage(4).sendToTarget();
				}
			});
			viewHolderHasRead.image_read_icon.setOnLongClickListener(new OnLongClickListener() {
				
				@Override
				public boolean onLongClick(View v) {
					handle.obtainMessage(6, position, 0).sendToTarget();
					return true;
				}
			});
		} else {
			final BookBean bean = list.get((imageUrl != null && imageUrl.size() > 0) ? position - 1 : position);
			if (convertView != null && convertView.getTag() instanceof ViewHolder) {
				holder = (ViewHolder) convertView.getTag();
			}else {
				convertView = LayoutInflater.from(context).inflate(R.layout.collect_book_item, null);
				holder = new ViewHolder();
				holder.image_book_face = (ImageView) convertView.findViewById(R.id.image_book_face);
				holder.cb_choose = (CheckBox) convertView.findViewById(R.id.cb_choose);
				holder.f_image_play = (ImageView) convertView.findViewById(R.id.f_image_play);
				holder.image_is_read = (ImageView) convertView.findViewById(R.id.image_is_read);
				holder.image_nation_book = (ImageView) convertView.findViewById(R.id.image_nation_book);
				convertView.setTag(holder);
			}
			if (bean.getBookId() > 0) {
				if (type == 2) {
					holder.image_is_read.setImageResource(R.drawable.yidu);
					holder.image_is_read.setVisibility(View.VISIBLE);
				}else {
					holder.image_is_read.setVisibility(View.GONE);
				}
				if (bean.getIsNew() == 2) {
					holder.image_nation_book.setVisibility(View.VISIBLE);
				}else {
					holder.image_nation_book.setVisibility(View.GONE);
				}
				holder.image_book_face.setVisibility(View.VISIBLE);
				holder.cb_choose.setVisibility(View.VISIBLE);
				
				if (position == 0 && bean.getIsRead() == 2) {
					holder.cb_choose.setVisibility(View.GONE);
				}else {
					if (isShowCheckBox) {
						holder.cb_choose.setVisibility(View.VISIBLE);
					}else {
						holder.cb_choose.setVisibility(View.GONE);
					}
					if (selectedBookIds.contains(bean.getBookId())) {
						holder.cb_choose.setChecked(true);
					}else {
						holder.cb_choose.setChecked(false);
					}
					holder.cb_choose.setTag(bean.getBookId());
					holder.cb_choose.setOnClickListener(new OnClickListener() {
						
						@Override
						public void onClick(View v) {
							int tempId = Integer.parseInt(v.getTag().toString());
							selectedItem(tempId);
						}
					});
					int playingBookId[] = MusicPlayManager.getInstance(context).isPlayingBookId();
					if (playingBookId[0] != 0 && playingBookId[0] == bean.getBookId()) {
						holder.f_image_play.setVisibility(View.VISIBLE);
						if (playingBookId[2] == 0){
							
							holder.f_image_play.setImageResource(R.drawable.playing);
						}else{
							holder.f_image_play.setImageResource(R.drawable.play);
						}
					}else {
						holder.f_image_play.setVisibility(View.GONE);
						holder.f_image_play.setImageResource(R.drawable.play);
					}
					holder.image_book_face.setOnClickListener(new OnClickListener() {
						
						@Override
						public void onClick(View v) {
							if (!isShowCheckBox) {
								handle.obtainMessage(5, position, 0).sendToTarget();
							}else {
								selectedItem(bean.getBookId());
								notifyDataSetChanged();
							}
						}
					});
					holder.image_book_face.setOnLongClickListener(new OnLongClickListener() {
						
						@Override
						public boolean onLongClick(View arg0) {
							handle.obtainMessage(6, position, 0).sendToTarget();
							return true;
						}
					});
					
				}
				ImageLoader.getInstance().displayImage(bean.getFaceImg(), holder.image_book_face);
			} else {
				holder.image_is_read.setVisibility(View.GONE);
				holder.image_book_face.setVisibility(View.GONE);
				holder.cb_choose.setVisibility(View.GONE);
				holder.f_image_play.setVisibility(View.GONE);
				holder.image_nation_book.setVisibility(View.GONE);
			}
		}
		return convertView;
	}
	
	public void selectedItem(int bookId){
		if (selectedBookIds.contains(bookId)) {
			selectedBookIds.remove(selectedBookIds.indexOf(bookId));
		}else {
			selectedBookIds.add(bookId);
		}
		handle.obtainMessage(2, selectedBookIds.size() , 0).sendToTarget();
	}
	
	public void itemClick(){
		if (isShowCheckBox) {
			holder.cb_choose.performClick();
		}
	}
	
	/**
	 * 全部勾选
	 */
	public void selectAllItem(){
		for (BookBean item : list) {
			if (!selectedBookIds.contains(item.getBookId()) && item.getIsRead() == 1) {
				selectedBookIds.add(item.getBookId());
			}
		}
		handle.obtainMessage(2, selectedBookIds.size() , 0).sendToTarget();
	}
	
	/**
	 * 清除勾选
	 */
	public void clearSelected(){
		selectedBookIds.clear();
		handle.obtainMessage(2, selectedBookIds.size() , 0).sendToTarget();
	}
	
	/**
	 * 添加勾选
	 * @param bookId
	 */
	public void addSelected(int bookId){
		if (!selectedBookIds.contains(bookId)) {
			selectedBookIds.add(bookId);
		}
	}
	
	/**
	 * 去除勾选
	 * @param bookId
	 */
	public void removeSelected(int bookId){
		if (selectedBookIds.contains(bookId)) {
			selectedBookIds.remove(selectedBookIds.indexOf(bookId));
		}
	}
	
	
	public boolean isShowCheckBox() {
		return isShowCheckBox;
	}

	public void setShowCheckBox(boolean isShowCheckBox) {
		this.isShowCheckBox = isShowCheckBox;
	}
	
	public ArrayList<Integer> getSelectedBookIds() {
		return selectedBookIds;
	}

	public void setSelectedBookIds(ArrayList<Integer> selectedBookIds) {
		this.selectedBookIds = selectedBookIds;
	}
	
	public class ViewHolder{
		public ImageView image_book_face,f_image_play,image_is_read,image_nation_book;
		public CheckBox cb_choose;
		
	}
	
	public class ViewHolderHasRead{
		public GridView gv_read_book;
		private ImageView image_read_icon;
	}

}
